package cn.tedu.egcar1.controller;

import cn.tedu.egcar1.base.response.JsonResult;
import cn.tedu.egcar1.mapper.UserMapper;
import cn.tedu.egcar1.pojo.dto.UserAddParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/v1/user/")
@ResponseBody
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Operation(summary = "用户注册")
    @Parameters({
            @Parameter(name = "username", description = "用户名", required = true),
            @Parameter(name = "password", description = "密码", required = true),
            @Parameter(name = "email", description = "邮箱", required = true),
            @Parameter(name = "phone", description = "手机号", required = true)
    })
    @PostMapping("reg")
    public JsonResult register(UserAddParam userAddParam){
        log.debug("用户注册参数："+userAddParam);
        userMapper.insertUser(userAddParam);
        return JsonResult.ok();
    }
    @Operation(summary = "用户登录")
    @Parameters({
            @Parameter(name = "username", description = "用户名", required = true),
            @Parameter(name = "password", description = "密码", required = true)
    })
    @PostMapping("login")
    public JsonResult login(@RequestBody UserAddParam userAddParam){
        log.debug("用户登录参数："+userAddParam);
        int count = userMapper.selectUsernameAndPassword(userAddParam.getUsername());
        if (count==0){
            return JsonResult.ok("用户名或密码错误");
        }
        return JsonResult.ok("用户登录成功");

    }


}
